package org.mobile.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mobile.logic.PhoneLogic;

/**
 * Servlet implementation class RemovePhone
 */
@WebServlet(name = "removePhone", urlPatterns = { "/removePhone" })
public class RemovePhone extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public RemovePhone() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int phoneID= Integer.parseInt(request.getParameter("phoneID"));
		if(!PhoneLogic.getInstance().removePhone(phoneID)){
			request.setAttribute("error", "Błąd usuwania telefonu!");
			request.getRequestDispatcher("errPage.jsp").forward(request, response);
		}
		else{
			request.getSession().setAttribute("phones", PhoneLogic.getInstance().getAllPhones());
			request.setAttribute("message", "Telefon został usunięty!");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		
	}

}
